//
//  NotificationController.swift
//  WatchLandmarks Watch App
//
//  Created by 符生军 on 2024/12/19.
//

import SwiftUI
import UserNotifications
import WatchKit

class NotificationController: WKUserNotificationHostingController<NotificationView> {
    var title: String?
    var message: String?
    var landmark: Landmark?
    let landmarkIndexKey = "landmarkIndex"
    
    override var body: NotificationView {
        NotificationView(
            title: title,
            message: message,
            landmark: landmark
        )
    }
    
    // 模拟发送通知
    // 在模拟器上运行APP之后，杀死APP
    // 然后把这个 xx.apns 文件拖动到模拟器上
    // 注意这个key "Simulator Target Bundle" 需要配置正确
    
    override func didReceive(_ notification: UNNotification) {
        let modelData = ModelData()
        let notificationData = notification.request.content.userInfo as? [String: Any]
        let aps = notificationData?["aps"] as? [String: Any]
        let alert = aps?["alert"] as? [String: Any]
        title = alert?["title"] as? String
        message = alert?["message"] as? String
        
        if let index = notificationData?[landmarkIndexKey] as? Int {
            landmark = modelData.landmarks[index]
        }
    }
}
